<?php

namespace common\models\position;

use Yii;
use common\helpers\Url;

/**
 * This is the model class for table "{{%position}}".
 *
 * @property int $id
 * @property string $name 推荐位名称
 * @property string $label 推荐位标识
 * @property string|null $group_name 分组标识
 * @property string|null $title 标题
 * @property string|null $description 简介
 * @property string|null $thumb 推荐位图示
 * @property int $width 推荐位宽度
 * @property int $height 推荐位高度
 * @property int $limits 推荐内容限制数目
 * @property int $listorder 排序
 * @property string|null $template 模板
 * @property int $map_id 内部路由ID
 * @property string|null $map_data 内部路由参数
 * @property string|null $model 模型
 * @property int $page_id 页面ID
 * @property int $status 推荐位状态：0禁用，10启用
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 * @property int $deleted_at 删除时间
 */
class Position extends \common\db\ActiveRecord {

    /**
     * 推荐位状态
     */
    const STATUS_ACTIVE = 10;
    const STATUS_DEFAULT = 0;
    //
    const MODEL_NONE = null;
    const MODEL_ARTICLE = 'article';
    const MODEL_ACTIVITY = 'activity';
    //
    const TEMPLATE_NONE = null;
    const TEMPLATE_CELL = 'cell';
    const TEMPLATE_MEDIA = 'media';
    const TEMPLATE_SWIPER = 'swiper';
    const TEMPLATE_SLIPCARD = 'slipcard';
    //
    const LABEL_ARTICLE = 'broadcast.home';

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%position}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['name', 'label'], 'required'],
            [['width', 'height', 'limits', 'listorder', 'map_id', 'page_id', 'status', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['name', 'label', 'group_name', 'title', 'template', 'model'], 'string', 'max' => 50],
            [['description', 'thumb', 'map_data'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'name' => '推荐位名称',
            'label' => '推荐位标识',
            'group_name' => '分组标识',
            'title' => '标题',
            'description' => '简介',
            'thumb' => '推荐位图示',
            'width' => '推荐位宽度',
            'height' => '推荐位高度',
            'limits' => '推荐内容限制数目',
            'listorder' => '排序',
            'template' => '模板',
            'map_id' => '内部路由ID',
            'map_data' => '内部路由参数',
            'model' => '模型',
            'page_id' => '页面ID',
            'status' => '推荐位状态：0禁用，10启用',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            'deleted_at' => '删除时间',
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function getData() {
        return $this->hasMany(PositionData::className(), ['position_id' => 'id']);
    }

    public function getActiveContent() {
        $query = $this->getContent()->active(PositionData::STATUS_ACTIVE)->orderBy([
            'listorder' => SORT_ASC,
        ]);
        if ($this->limits > 0) {
            $query->limit($this->limits);
        }
        return $query;
    }

    /**
     * {@inheritdoc}
     */
    public function getList() {
        $query = $this->getData()->active(PositionData::STATUS_ACTIVE);
        if ($this->imageFormat) {
            $query->andWhere([
                'and',
                ['!=', 'thumb', ''],
                ['is not', 'thumb', null],
            ]);
        }
        $query->orderBy([
            'listorder' => SORT_ASC,
        ]);
        $query->limit($this->limits);
        $query->debug();
        return $query;
    }

    /**
     * 是否图片展位
     * @return bool
     */
    public function getImageFormat() {
        if ($this->width || $this->height) {
            return true;
        }
        return false;
    }

    /**
     * 图片地址
     * @return string
     */
    public function getThumbUrl() {
        return Url::attach($this->thumb);
    }

    /**
     * 图片缩放比例
     * @return integer
     */
    public function getScale() {
        if ($this->width && $this->height) {
            return $this->width . ' * ' . $this->height;
        }
        if ($this->width || $this->height) {
            return ($this->width ?: 'auto') . ' * ' . ($this->height ?: 'auto' );
        }
        return '无需图片';
    }

    /**
     * {@inheritdoc}
     */
    public static function findByName($name) {
        return static::find()->active(static::STATUS_ACTIVE)->andWhere(['name' => $name])->one();
    }

}
